package com.yxt.user.service.impl;

import com.yxt.common.utils.JwtUtil;
import com.yxt.common.utils.R;
import com.yxt.user.config.ResourceServerConfig;
import com.yxt.user.entity.RoleEntity;
import com.yxt.user.entity.UserEntity;
import com.yxt.user.entity.UserRoleEntity;
import com.yxt.user.mapper.RoleMapper;
import com.yxt.user.mapper.UserMapper;
import com.yxt.user.mapper.UserRoleMapper;
import com.yxt.user.service.RoleService;
import com.yxt.user.service.UserRoleService;
import com.yxt.user.service.UserService;
import com.yxt.user.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxt.common.utils.PageUtils;
import com.yxt.common.utils.Query;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R getUserInfo() {
        OAuth2AuthenticationDetails authentication = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        Map<String, String> stringStringMap = JwtUtil.decodeToken(authentication.getTokenValue(), ResourceServerConfig.PUBLIC_KEY);

        String userName = stringStringMap.get("user_name");
        //获取到用户id
        UserEntity userEntity = this.getOne(new QueryWrapper<UserEntity>().eq("username", userName));

        List<UserRoleEntity> userRoleIds = userRoleMapper.selectList(new QueryWrapper<UserRoleEntity>().eq("user_id", userEntity.getUserId()));

        List<Long> ids = userRoleIds.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());

        List<RoleEntity> roleEntities = roleMapper.selectBatchIds(ids);


        List<String> roleNames = roleEntities.stream().map(item -> {

            return item.getRoleName();
        }).collect(Collectors.toList());

        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setAvatar(userEntity.getAvatar());
        userInfoVO.setIntroduction(userEntity.getIntroduction());
        userInfoVO.setName(userEntity.getUsername());
        userInfoVO.setRoles(roleNames);
        return R.ok().put("data",userInfoVO);
    }

}